package com.xia.service.impl;

import com.xia.dao.article.IArticleDao;
import com.xia.dao.article.ILikeArticleDao;
import com.xia.dao.article.IVideoDao;
import com.xia.domian.*;
import com.xia.service.IArticleService;
import com.xia.service.ICommentService;
import com.xia.util.exception.CodeEnum;
import com.xia.util.exception.ServiceException;
import com.xia.util.websocket.Websocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.soap.Node;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 文章业务实现层
 */
@Service
public class IArticleServiceImpl implements IArticleService {
    private ICommentService commentService;
    private IArticleDao articleDao;
    private ILikeArticleDao likeArticleDao;
    private IVideoDao videoDao;
    @Resource
    private String getDate;
    @Value("${online.leading-end-agreement}")
    private String leadingEndAgreement;

    @Autowired
    public void setVideoDao(IVideoDao videoDao) {
        this.videoDao = videoDao;
    }
    @Autowired
    public void setCommentService(ICommentService commentService) {
        this.commentService = commentService;
    }
    @Autowired
    public void setLikeArticleDao(ILikeArticleDao likeArticleDao) {
        this.likeArticleDao = likeArticleDao;
    }
    @Autowired
    public void setArticleDao(IArticleDao articleDao) {
        this.articleDao = articleDao;
    }

    @Override
    public Map<String, Object> searchArticle(String searchContent) {
        Map<String, Object> map = new HashMap<>();
        List<Article> articleList = articleDao.selectLikeArticle(searchContent);
        //已查询到的帖子数量
        Integer articleNum=0;
        //已查询到的文章数量
        Integer postNum=0;
        for (Article article : articleList) {
            //通过循环查询数据中辨识是否为帖子或文章增加数量
            if (article.getArticlePost() == 2) {
                articleNum++;
            } else {
                postNum++;
            }
        }
        map.put("articleNum",articleNum);
        map.put("postNum",postNum);
        map.put("size",articleList.size());
        map.put("multipleArticleList", articleList);
        return map;
    }

    public List<Article> articlesUtil(Integer communityId, Integer forLength) {
        List<Article> articleList = new ArrayList<>();
        //将随机后的文章ID放入集合
        List<Integer> articleId = new ArrayList<>();
        //获取所有文章ID 若传了社区ID则根据社区来获取
        List<Integer> articleCount = articleDao.selectArticleId(communityId, 2);
        Random random = new Random();
        //将随机的list下标放入set
        Set<Integer> articleDataSet = new HashSet<Integer>();
        for (int i = 1; i <= forLength; i++) {
            //将list下标随机产出
            int data = random.nextInt(articleCount.size());
            //判断随机数与set中的数据是否重复，未添加成功则再循环一遍
            if (!articleDataSet.add(data)) {
                i--;
            }
        }
        //根据将随机后的下标把文章ID放入list中
        for (Integer setData : articleDataSet) {
            articleId.add(articleCount.get(setData));
        }
        //根据文章ID获取数据并获取各文章评论数量
        for (Integer integer : articleId) {
            List<Article> list = selectArticleSizeUtil(2, integer, 0, 0);
            articleList.addAll(list);
        }
        return articleList;
    }

    @Override
    public Map<String, Object> selectMsgArticle(Integer usersId) {
        Map<String, Object> map = new HashMap<>();
        List<Article> postList = selectArticleSizeUtil(1,0,0,usersId);
        List<Article> articleList = selectArticleSizeUtil(2,0,0,usersId);
        map.put("postList", postList);
        map.put("articleList", articleList);
        return map;
    }

    public List<Article> selectArticleSizeUtil(Integer articlePost,Integer articleId,Integer communityId,Integer usersId) {
        List<Article> list = articleDao.selectArticleOnId(articlePost, articleId, communityId, usersId);
            for (Article article : list) {
                Map<String, Object> mapComment = commentService.selectArticleComment(article.getArticleId(),1);
                Integer commentListSize = (Integer) mapComment.get("commentListSize");
                if (commentListSize == null) {
                    article.setCommentSize(0);
                } else {
                    article.setCommentSize(commentListSize);
                }
            }
        return list;
    }

    @Override
    public List<Article> selectArticleCommentList() {
        List<Article> list=articleDao.selectHotList();
        if (list.size() < 10){
            //当热榜不足10条则获取其他文章信息
            List<Integer> integerList=new ArrayList<>();
            for (Article article : list) {
                integerList.add(article.getArticleId());
            }
            Map<String,Object> map=new HashMap<>();
            map.put("articleId",integerList);
            map.put("amount",10-list.size());
            List<Article> articleList=articleDao.selectNotInArticleId(map);
            list.addAll(articleList);
        }
        return list;
    }

    @Override
    public void likeArticleOperation(LikeArticle likeArticle) throws IOException{
        Integer likeArticleId= likeArticleDao.isSelectLikeArticle(likeArticle.getLikeArticle(),likeArticle.getLikeUsers());
        Article article = new Article();
        article.setArticleId(likeArticle.getLikeArticle());
        article.setArticleLike(likeArticle.getArticleLike());
        articleDao.updateArticle(article);
        if (likeArticleId != null){
            likeArticle.setLikeCreatingTime(getDate);
            likeArticle.setLikeId(likeArticleId);
            likeArticleDao.updateLikeArticle(likeArticle);
            if (likeArticle.getLikeState() == 1){
                Article articleLike=articleDao.selectArticleContentId(likeArticle.getLikeArticle());
                Websocket.sendMessage(String.valueOf(articleLike.getArticleAuthor()),"有人点赞了你的作品哦");
            }
            return;
        }
        likeArticle.setLikeCreatingTime(getDate);
        Article articleLike=articleDao.selectArticleContentId(likeArticle.getLikeArticle());
        Websocket.sendMessage(String.valueOf(articleLike.getArticleAuthor()),"有人点赞了你的作品哦");
        likeArticleDao.insertLike(likeArticle);
    }

    @Override
    public void issueVideo(Video video) {
        video.setVideoCreatingTime(getDate);
        videoDao.insertVideo(video);
    }

    @Override
    public void writeArticle(Article article) {
        article.setArticleCreatingTime(getDate);
        if (article.getArticleFirstImage()==null){
            article.setArticleFirstImage(leadingEndAgreement+"/static/img/推荐.jpg");
        }
        if (articleDao.insertArticle(article) == 0){
            throw new ServiceException(CodeEnum.CODE_400,"发布失败");
        }
    }

}
